package com.topscomm.main.config;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.topscomm.interceptor.MmsAccessInterceptor;

/**
 * @description:webmvc配置
 * @author: zhangsui
 * @date: 2019年3月16日上午10:22:59
 * @modify:
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
	@Value("${spring.profiles.active}")
	private String profiles;
	@Autowired
	MmsAccessInterceptor mmsAccessInterceptor;
	/**
	 * @description:跨域处理
	 * @param registry
	 * @author: zhangsui
	 * @date: 2019年3月15日下午9:51:49
	 * @modify:
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**").allowCredentials(true).allowedHeaders("*").allowedOrigins("*").allowedMethods("GET", "POST", "PUT", "DELETE");
	}

	/**
	 * @description:资源访问
	 * @param registry
	 * @author: zhangsui
	 * @date: 2019年3月15日下午9:52:00
	 * @modify:
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
	}

	@Bean
	public HttpMessageConverter<String> responseBodyConverter() {
		StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
		return converter;
	}

	/**
	 * @author zhanglongfei
	 * @date 2020年3月2日
	 * @description 更换json转换器为fastJson，用于设置部分特殊类型格式
	 * @return
	 */
	@Bean
	public FastJsonHttpMessageConverter fastJsonHttpMessageConverters() {
		// 1.定义一个converters转换消息的对象
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		// 2.添加fastjson的配置信息，比如: 是否需要格式化返回的json数据
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		if ("prod".equals(profiles)) {
			fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue);
		} else {
			fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
		}
		fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
		// 3.在converter中添加配置信息
		fastConverter.setFastJsonConfig(fastJsonConfig);
		// 5.返回HttpMessageConverters对象
		return fastConverter;
	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		converters.add(responseBodyConverter());
		converters.add(fastJsonHttpMessageConverters());
	}

	@Override
	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
		configurer.favorPathExtension(false);
	}
	/**
	 * 添加拦截器
	 * @description: 
	 * @param registry 
	 * @author: hudeyong
	 * @date: 2021年06月21日上午9:40:25
	 * @modify:
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		List<String> excludePathPatterns = new ArrayList<String>();
		List<String> addPathPatterns = new ArrayList<String>();
		//两表电业务脑端和手机端相关接口
		addPathPatterns.add("/msp/**");
		addPathPatterns.add("/ltc/**");
		//电脑端首页
		addPathPatterns.add("/cbo/approvalNodeIns/queryApprovalList");
		//手机端审批拦截接口
		addPathPatterns.add("app/appapproval/myApprovalList");
		addPathPatterns.add("app/appapproval/listHistoryRecord");
		//排除相关日志
		excludePathPatterns.add("/**/**/queryInitData");
		excludePathPatterns.add("/**/**/dataCodeList");
		excludePathPatterns.add("/**/**/createUid");
		excludePathPatterns.add("/**/**/queryBelongCfoDepts");
		excludePathPatterns.add("/mms/userAccessInfo/**");
		registry.addInterceptor(mmsAccessInterceptor).addPathPatterns(addPathPatterns).excludePathPatterns(excludePathPatterns);
	}
}
